import 'dart:async';
import 'package:deer/login/login_router.dart';
import 'package:deer/routers/application.dart';
import 'package:deer/routers/fluro_navigator.dart';
import 'package:deer/util/utils.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class WelComePage extends StatefulWidget {
  @override
  _WelComePage createState() => _WelComePage();
}

class _WelComePage extends State<WelComePage> {
  int _status = 0;
  List<String> _guideList = [
    "app_start_1",
    "app_start_2",
    "app_start_3",
  ];

  StreamSubscription _subcription;

  @override
  void initState() {
    super.initState();
    _run();
  }

  void _initAsync() async {
    // await
    // _initGuide();
    _goLogin();
  }

  void _initGuide() {
    setState(() {
      _status = 1;
    });
  }

  _goLogin() {
    NavigatorUtils.push(context, LoginRouter.loginPage,
        replace: true,
        transitionType: TransitionType.cupertinoFullScreenDialog);
  }

  void _run() {
    _subcription =
        Observable.just(1).delay(Duration(milliseconds: 1000)).listen((_) {
      _initAsync();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Stack(
        children: <Widget>[
          Offstage(
            offstage: !(_status == 0), // 是否隐藏
            child: Image.asset(
              Utils.getImagePath('start_page', format: 'jpg'),
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.fill,
            ),
          ),
          Offstage(
            offstage: !(_status == 1),
            child: Swiper(
              itemCount: _guideList.length,
              loop: false,
              itemBuilder: (_, index) {
                return Image.asset(
                  Utils.getImagePath(_guideList[index]),
                  fit: BoxFit.fill,
                  width: double.infinity,
                  height: double.infinity,
                );
              },
              onTap: (index) {
                if (index == _guideList.length - 1) {
                  _goLogin();
                }
              },
            ),
          )
        ],
      ),
    );
  }
}
